package com.weijin.springbootbase.controller;

import com.weijin.springbootbase.common.PageResult;
import com.weijin.springbootbase.common.Result;
import com.weijin.springbootbase.pojo.User;
import com.weijin.springbootbase.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author WeiJin
 * @Version 1.0
 * @Date 2025/3/11 10:56
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户控制类")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/query/{id}")
    @ApiOperation("根据用户id查询单个用户信息")
    public Result<User> queryById(@PathVariable("id") Integer id) {
        return userService.queryById(id);
    }

    @GetMapping("/query")
    @ApiOperation("查询所有用户")
    public Result<List<User>> queryAll() {
        return userService.queryAll();
    }

    @PostMapping("/add")
    @ApiOperation("添加一个用户信息")
    public Result<String> addUser( @RequestBody User user) {
        return userService.addUser(user);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据用户Id删除用户信息")
    public Result<String> deleteById(@PathVariable("id") Integer id) {
        return userService.deleteById(id);
    }

    @PutMapping("/modify")
    @ApiOperation("修改用户信息")
    public Result<String> modifyUser( @RequestBody User user) {
        return userService.modifyUser(user);
    }

    @DeleteMapping("/delete/batch")
    @ApiOperation("批量删除")
    public Result<String> deleteBatchIds(Integer[] ids){
        return userService.deleteBatchIds(ids);
    }

    @GetMapping("/query/page")
    @ApiOperation("分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",dataType = "Integer",defaultValue = "1",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",dataType = "Integer",defaultValue = "5",paramType = "query")
    })
    public Result<PageResult<User>> queryPage(
            @RequestParam(value = "pageNum" ,required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize) {
        return userService.queryPage(pageNum,pageSize);
    }

    @GetMapping("/query/redis/{id}")
    @ApiOperation("验证redis缓存查询")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    public Result<User> queryRedisById(@PathVariable("id") Integer id){
        return userService.queryRedisById(id);
    }

}
